package com.ymttest.database.mongo;

import java.util.ArrayList;
import java.util.List;

import com.mongodb.MongoClientURI;
import com.ymt.core.tool.FormatUtil;
import com.ymt.database.MongoDBDriver;
import com.ymttest.utils.EnvSetup;

public class DeviceidWapper {
	MongoDBDriver mdd;

	public DeviceidWapper() throws Exception{
		mdd = new MongoDBDriver(new MongoClientURI(EnvSetup.getSystemUrl("set5_deviceinfodbmongourl")));
		mdd.selectCollection("deviceId");	
	}

	/**
	 * 用uid查询AccessToken
	 * 
	 * @param uid
	 * @return AccessToken
	 * @throws Exception
	 */
	public List<DeviceidBean> getByDeviceid(String deviceid) throws Exception {
	
		String re = null;
		List<DeviceidBean> mbs = new ArrayList<>();
		
		DeviceidBean mb = null;
		
		ArrayList<String> list = mdd.selectPartLimit("{\"deviceid\":\"" + deviceid + "\"}",100);
		try {
			for(int i=0;i<list.size();i++) {
				re = list.get(i);
				mb = (DeviceidBean) FormatUtil.GSONToObject(re, DeviceidBean.class);
				mbs.add(mb);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		mdd.close();
		return mbs;
	}
	
	/**
	 * 根据deviceId修改时间
	 * 
	 * @param uid
	 * @return AccessToken
	 * @throws Exception
	 */
	public void updateByDeviceId(String deviceid, String addtime) throws Exception {

		mdd.update("{\"deviceid\":\"" + deviceid + "\"}", "{\"activeTime\":{$date:\"" + addtime + "\"}}");
		
		mdd.close();
	}
	
	public static void main(String[] args) throws Exception {
		DeviceidWapper deviceidWapper = new DeviceidWapper();
		deviceidWapper.updateByDeviceId("2a8b2032-b899-41b6-9044-40f7a00c5e83", "2016-05-16T07:01:05.499Z");
	}
}
